<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" >
        <a-row>
          <a-divider> <span style="font-size: 12px;">基础信息</span></a-divider>
          <a-col :span="6" >
            <a-form-model-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入商品名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="供货商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantInfoId">
              <j-dict-select-tag type="list" v-model="model.merchantInfoId" dictCode="xs_merchant_info,mer_name,id" placeholder="请选择供货商" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subTitle">
              <a-input v-model="model.subTitle" placeholder="请输入副标题" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditStatus">
              <j-dict-select-tag type="list" v-model="model.auditStatus" dictCode="xs_audit_status" placeholder="请选择审核状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="类目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tradeMenuId">
              <j-dict-select-tag type="list" v-model="model.tradeMenuId" dictCode="xs_trade_menu,name,id" placeholder="请选择行业" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productLabel">
              <a-input v-model="model.productLabel" placeholder="请输入标签" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useStatus">
              <j-dict-select-tag type="list" v-model="model.useStatus" dictCode="xs_use_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="第三方商品编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherNo">
              <a-input v-model="model.otherNo" placeholder="请输入第三方商品编码" ></a-input>
            </a-form-model-item>
          </a-col>

          <a-divider> <span style="font-size: 12px;">价格信息</span></a-divider>

          <a-col :span="6" >
            <a-form-model-item label="原价(划线价)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="costPrice">
              <a-input-number v-model="model.costPrice" placeholder="请输入商品原价/划线价（单位：元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="最低售价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainPrice">
              <a-input-number v-model="model.mainPrice" placeholder="请输入最低售价（单位：元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="平台进货价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyingPrice">
              <a-input-number v-model="model.buyingPrice" placeholder="请输入平台进货价（单位：元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="结算价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="platPrice">
              <a-input-number v-model="model.platPrice" placeholder="请输入结算价（单位：元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-divider> <span style="font-size: 12px;">其他信息</span></a-divider>

          <a-col :span="6" >
            <a-form-model-item label="配送规则" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deliveryRole">
              <a-input v-model="model.deliveryRole" placeholder="仅私域主可见" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="保质期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quality">
              <a-input v-model="model.quality" placeholder="请输入保质期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="商品规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="specs">
              <a-input v-model="model.specs" placeholder="例：张 个 份 斤 等" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6" >
            <a-form-model-item label="打包规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="baleSpecs">
              <a-input v-model="model.baleSpecs" placeholder="例：2斤一份 十瓶一份 等" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-col :span="6" >
          <a-form-model-item label="商品主图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainImg">
            <j-image-upload isMultiple  v-model="model.mainImg" ></j-image-upload>
          </a-form-model-item>
        </a-col>
        <a-row>
          <a-col :span="24" >
            <a-form-model-item labelAlign="left" label="注意事项" :labelCol="labelColOne" :wrapperCol="wrapperCol2" prop="needMess">
              <a-textarea v-model="model.needMess" rows="2" placeholder="请输入注意事项" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item  labelAlign="left" label="商品产地" :labelCol="labelColOne" :wrapperCol="wrapperCol2" prop="originAddress">
              <a-textarea v-model="model.originAddress" rows="2" placeholder="请输入商品产地" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" >
            <a-form-model-item labelAlign="left" label="使用规则" :labelCol="labelColOne" :wrapperCol="wrapperCol2" prop="useRole">
              <j-editor v-model="model.useRole" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" >
            <a-form-model-item labelAlign="left" label="商品介绍" :labelCol="labelColOne" :wrapperCol="wrapperCol2" prop="productDescription">
              <j-editor v-model="model.productDescription" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="商品文案" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="productClerkTable.loading"
          :columns="productClerkTable.columns"
          :dataSource="productClerkTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="商品图集" :key="refKeys[1]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="producImgTable.loading"
          :columns="producImgTable.columns"
          :dataSource="producImgTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ProductInfoForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        labelColOne: {
          xs: { span: 2 },
          sm: { span: 2 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           merchantInfoId: [
              { required: true, message: '请输入供货商!'},
           ],
           name: [
              { required: true, message: '请输入商品名称!'},
           ],
        },
        refKeys: ['productClerk', 'producImg', ],
        tableKeys:['productClerk', 'producImg', ],
        activeKey: 'productClerk',
        // 商品文案
        productClerkTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '文案',
              key: 'clerk',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 商品图集
        producImgTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '图片地址',
              key: 'imgUrl',
              type: FormTypes.image,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/xs/productInfo/add",
          edit: "/xs/productInfo/edit",
          queryById: "/xs/productInfo/queryById",
          productClerk: {
            list: '/xs/productInfo/queryProductClerkByMainId'
          },
          producImg: {
            list: '/xs/productInfo/queryProducImgByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.productClerkTable.dataSource=[]
        this.producImgTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.productClerk.list, params, this.productClerkTable)
          this.requestSubTableData(this.url.producImg.list, params, this.producImgTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          productClerkList: allValues.tablesValue[0].values,
          producImgList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>